<template>
  <div class="login-container">
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form" auto-complete="on" label-position="left">

      <div class="title-container">
        <h3 class="title">
          <img src="@/assets/common/login-logo.png" alt="">
        </h3>
      </div>

      <el-form-item prop="mobile">
        <span class="svg-container">
          <svg-icon icon-class="user" />
        </span>
        <el-input
          ref="mobile"
          v-model="loginForm.mobile"
          placeholder="请输入手机号"
          name="mobile"
          type="text"
          tabindex="1"
          auto-complete="on"
        />
      </el-form-item>

      <el-form-item prop="password">
        <span class="svg-container">
          <svg-icon icon-class="password" />
        </span>
        <el-input
          :key="passwordType"
          ref="password"
          v-model="loginForm.password"
          :type="passwordType"
          placeholder="Password"
          name="password"
          tabindex="2"
          auto-complete="on"
          @keyup.enter.native="handleLogin"
        />
        <span class="show-pwd" @click="showPwd">
          <svg-icon :icon-class="passwordType === 'password' ? 'eye' : 'eye-open'" />
        </span>
      </el-form-item>

      <!--
        事件修饰符:
        prevent: e.preventDefault()
        native: 将原生事件绑定到组件的根元素

        如果组件内部支持了this.$emit('click')原生事件,就不用写了

        <jack @test='xxx'></jack>
       -->
      <el-button
        class="loginBtn"
        :loading="loading"
        type="primary"
        style="width:100%;margin-bottom:1.875rem;"
        @click.native.prevent="handleLogin"
      >登录</el-button>

      <div class="tips">
        <span style="margin-right:1.25rem;">账号: 13800000002</span>
        <span> 密码: 123456</span>
      </div>

    </el-form>
  </div>
</template>

<script>
import { validMobile } from '@/utils/validate'
import { mapActions } from 'vuex'

export default {
  name: 'Login',
  data() {
    // const validateUsername = (rule, value, callback) => {
    //   if (!validUsername(value)) {
    //     callback(new Error('Please enter the correct user name'))
    //   } else {
    //     callback()
    //   }
    // }
    const validatePassword = (rule, value, callback) => {
      if (value.length < 6) {
        callback(new Error('密码不能小于6位'))
      } else {
        callback()
      }
    }
    const validateMobile = (rule, value, callback) => {
      if (!validMobile(value)) {
        callback(new Error('请输入正确的手机号'))
      } else {
        callback()
      }
    }
    return {
      loginForm: {
        mobile: '13800000002',
        password: '123456'
      },
      loginRules: {
        mobile: [
          { required: true, message: '请输入手机号', trigger: 'blur' },
          { validator: validateMobile, message: '请输入正确手机号', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { validator: validatePassword, message: '密码长度不能小于6位', trigger: 'blur' }
        ]
      },
      loading: false,
      passwordType: 'password',
      redirect: undefined
    }
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect
      },
      immediate: true
    }
  },
  methods: {
    ...mapActions('user', ['login']),
    // ...mapActions(['user/login']),
    showPwd() {
      if (this.passwordType === 'password') {
        this.passwordType = ''
      } else {
        this.passwordType = 'password'
      }
      this.$nextTick(() => {
        this.$refs.password.focus()
      })
    },
    handleLogin() {
      this.$refs.loginForm.validate(async flag => {
        if (!flag) return
        // 收集数据 ajax准备
        // console.log(this.loginForm)

        // this.login(this.loginForm)
        // this.$router.push('/')

        // 有两个问题:
        // 1.this.$router.push()上面的this.login方法是异步,不等待
        // 2.用户名或密码是错误的也能跳转首页

        try {
          // 开始加载
          this.loading = true

          await this.login(this.loginForm)
          this.$message.success('恭喜,登录成功')
          this.$router.push('/')
        } catch (err) {
          console.log(err)
        } finally {
          this.loading = false
        }

        // this.login(this.loginForm).then((res) => {
        //   // console.log(res)
        //   // this.$router.push('/')
        //   this.$message.success('恭喜,登录成功')
        //   this.$router.push('/')
        //   // if (res.data.success) {
        //   //   this.$message.success('恭喜,登录成功')
        //   //   this.$router.push('/')
        //   // } else {
        //   //   this.$message.error('登录失败')
        //   // }
        // }).catch((error) => {
        //   console.log(error)
        //   // this.$message.error('登录失败2')
        // })
      })
    }
  }
}
</script>

<style lang="scss">
/* 修复input 背景不协调 和光标变色 */
/* Detail see https://github.com/PanJiaChen/vue-element-admin/pull/927 */

$bg:#283443;
$light_gray: #407ffe; // 修改输入框字体颜色
$cursor: #fff;

@supports (-webkit-mask: none) and (not (cater-color: $cursor)) {
  .login-container .el-input input {
    color: $cursor;
  }
}

/* reset element-ui css */
.login-container {
  // @在css中作为路径 前面要加~ 它才会认为 /src
  background-image: url('~@/assets/common/login.jpg'); // 设置背景图片
  background-position: center; // 将图片位置设置为充满整个屏幕
  .el-input {
    display: inline-block;
    height: 2.9375rem;
    width: 85%;

    input {
      background: transparent;
      border: 0rem;
      -webkit-appearance: none;
      border-radius: 0rem;
      padding: .75rem .3125rem .75rem .9375rem;
      color: $light_gray;
      height: 2.9375rem;
      caret-color: $cursor;

      &:-webkit-autofill {
        box-shadow: 0 0 0rem 62.5rem $bg inset !important;
        -webkit-text-fill-color: $cursor !important;
      }
    }
  }

  .el-form-item {
    border: .0625rem solid rgba(255, 255, 255, 0.1);
    background: rgba(255, 255, 255, 0.9); // 输入登录表单的背景色
    border-radius: .3125rem;
    color: #454545;
  }

  .el-form-item__error {
    color: #fff;
    font-size: .875rem;
  }

  .loginBtn {
    background: #407ffe;
    height: 4rem;
    line-height: 2rem;
    font-size: 1.5rem;
  }
}
</style>

<style lang="scss" scoped>
$bg:#2d3a4b;
$dark_gray:#889aa4;
$light_gray:#eee;

.login-container {
  min-height: 100%;
  width: 100%;
  background-color: $bg;
  overflow: hidden;

  .login-form {
    position: relative;
    width: 32.5rem;
    max-width: 100%;
    padding: 10rem 2.1875rem 0;
    margin: 0 auto;
    overflow: hidden;
  }

  .tips {
    font-size: .875rem;
    color: #fff;
    margin-bottom: .625rem;

    span {
      &:first-of-type {
        margin-right: 1rem;
      }
    }
  }

  .svg-container {
    padding: .375rem .3125rem .375rem .9375rem;
    color: $dark_gray;
    vertical-align: middle;
    width: 1.875rem;
    display: inline-block;
  }

  .title-container {
    position: relative;

    .title {
      font-size: 1.625rem;
      color: $light_gray;
      margin: 0rem auto 2.5rem auto;
      text-align: center;
      font-weight: bold;
    }
  }

  .show-pwd {
    position: absolute;
    right: .625rem;
    top: .4375rem;
    font-size: 1rem;
    color: $dark_gray;
    cursor: pointer;
    user-select: none;
  }
}
</style>
